﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.CanGongService;
using CommunitySystem.Views;

namespace CommunitySystem
{
    public partial class CanGongDetailControl : AsyncCtrl
    {
        public int OrgID;
        public string StatusS = "";
        public CanGongService.CanGongServiceClient client = new CanGongServiceClient();
        public ObservableCollection<CanGongDetail> models = new ObservableCollection<CanGongDetail>();
        public ObservableCollection<CanGongDetail> selectmodels = new ObservableCollection<CanGongDetail>();
        public CanGongDetailControl(int orgid,string status)
        {
            InitializeComponent();
            OrgID = orgid;
            StatusS = status;
            client.GetCanGongDetailModelCompleted += new EventHandler<GetCanGongDetailModelCompletedEventArgs>(client_GetCanGongDetailModelCompleted);
            client.DeleteCanGongDetailCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_DeleteCanGongDetailCompleted);
            client.UpdateCheGaiDetailStatusCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_UpdateCheGaiDetailStatusCompleted);
            Load();
        }
        void client_GetCanGongDetailModelCompleted(object sender, GetCanGongDetailModelCompletedEventArgs e)
        {
            if(e.Result != null)
            {
                DGRole_admin.ItemsSource = e.Result;
            }
        }

        public void Load()
        {
            client.GetCanGongDetailModelAsync(OrgID,0,0);
        }

        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            if (StatusS == "已过渡")
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("该人员单位已过渡，不可对人员进行新增！");
                return;
            }
            CanGongDetailWindow window = new CanGongDetailWindow(0, true, OrgID);
            window.Closed += new EventHandler(RMWindow_Closed);
            window.Show();
        }
        void RMWindow_Closed(object sender, EventArgs e)
        {
            Load();
        }
        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            if(StatusS == "已过渡")
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("该人员单位已过渡，不可对人员进行编辑！");
                return;
            }
            selectmodels.Clear();
            if (DGRole_admin.ItemsSource != null)
            {
                foreach (CanGongDetail i in (ObservableCollection<CanGongDetail>)DGRole_admin.ItemsSource)
                {
                    if (i.IsSelected == true && selectmodels.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectmodels.Add(i);
                    }
                    if (selectmodels.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectmodels.Remove(selectmodels.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectmodels.Count > 0)
            {
                if(selectmodels.Count > 1)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("只可以对一个人员进行编辑！");
                    return;
                }
                if (selectmodels[0].Status == 1)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("该人员信息已提交，不可对人员进行编辑！");
                    return;
                }
                CanGongDetailWindow window = new CanGongDetailWindow(selectmodels[0].ID, false, OrgID);
                window.Closed += new EventHandler(RMWindow_Closed);
                window.Show();
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择一个人员进行编辑！");
            }
        }

        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            if (StatusS == "已过渡")
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("该人员单位已过渡，不可对人员进行删除！");
                return;
            }
            selectmodels.Clear();
            if (DGRole_admin.ItemsSource != null)
            {
                foreach (CanGongDetail i in (ObservableCollection<CanGongDetail>)DGRole_admin.ItemsSource)
                {
                   if(i.Status == 1)
                   {
                       SilverlightMessageBoxLibrary.Message.ErrorMessage("所选人员信息已提交，不可对人员进行删除！");
                       selectmodels.Clear();
                       foreach (CanGongDetail ii in (ObservableCollection<CanGongDetail>)DGRole_admin.ItemsSource)
                       {
                           ii.IsSelected = false;
                       }
                       return;
                   }
                    if (i.IsSelected == true && selectmodels.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectmodels.Add(i);
                    }
                    if (selectmodels.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectmodels.Remove(selectmodels.Where(p => p.ID == i.ID).FirstOrDefault());
                    }

                }
            }
            if (selectmodels.Count > 0)
            {
                if (MessageBox.Show("确定删除该人员吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    string ids = "";
                    foreach (CanGongDetail i in selectmodels)
                    {
                        ids += i.ID + ",";
                    }
                    client.DeleteCanGongDetailAsync(ids.Substring(0, ids.Length - 1));
                }
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择人员进行删除！");
            }
        }
        void client_DeleteCanGongDetailCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功！");
            Load();
        }
        private void TiJiaoData_Click(object sender, RoutedEventArgs e)
        {
            if (StatusS == "已过渡")
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("该人员单位已过渡，不可对人员进行提交！");
                return;
            }
            selectmodels.Clear();
            if (DGRole_admin.ItemsSource != null)
            {
                foreach (CanGongDetail i in (ObservableCollection<CanGongDetail>)DGRole_admin.ItemsSource)
                {
                    if (i.IsSelected == true && selectmodels.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectmodels.Add(i);
                    }
                    if (selectmodels.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectmodels.Remove(selectmodels.Where(p => p.ID == i.ID).FirstOrDefault());
                    }

                }
            }
            if (selectmodels.Count > 0)
            {
                string ids = "";
                foreach (CanGongDetail i in selectmodels)
                {
                    ids += i.ID + ",";
                }
                client.UpdateCheGaiDetailStatusAsync(ids.Substring(0, ids.Length - 1), "提交");
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择人员进行提交！");
            }
        }
        void client_UpdateCheGaiDetailStatusCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("提交成功！");
            Load();
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (CanGongDetail p in DGRole_admin.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;
                //CheckBox cb1 = DGRole.Columns[0].GetCellContent(p) as CheckBox;
                //cb1.IsChecked = p.IsSelected;
                if (p.IsSelected)
                {
                    if (selectmodels.Where(i => i.ID == p.ID).FirstOrDefault() == null)
                    {
                        selectmodels.Add(p);
                    }
                }
                else
                {
                    if (selectmodels.Where(i => i.ID == p.ID).FirstOrDefault() != null)
                    {
                        selectmodels.Remove(selectmodels.Where(i => i.ID == p.ID).FirstOrDefault());
                    }
                }
            }


        }
    }
}
